<?php

namespace app\shop\controller\product;

use app\common\model\store\Store;
use app\shop\controller\Controller;
use app\shop\model\product\ProductLabel as LabelModel;


/**
 * 标签管理控制器
 */
class ProductLabel extends Controller
{
    /**
     * 获取列表
     */
    public function index()
    {
        // 列表
        $list = LabelModel::listData($this->request->param(), $this->getData(), ['proxy','supplier']);

        $store_list = Store::where('is_delete',0) -> field('store_name,shop_supplier_id') -> select();
        $store_array = [];
        foreach ($store_list as $v){
            $store_array[$v['shop_supplier_id']][] = $v['store_name'];
        }
        foreach ($list as $key => $value){
            $list[$key]['store_name'] = $store_array[$value['shop_supplier_id']]??'';
        }
        $model = new LabelModel;

        // 数量
        $category_count = [
            'using'   => $model->getTotalByWhere(['audit_status' => 1]),
            'audit'   => $model->getTotalByWhere(['audit_status' => 0]),
            'no_pass' => $model->getTotalByWhere(['audit_status' => 2]),
        ];
        return $this->renderSuccess('', compact('list', 'category_count'));
    }

    /**
     * 添加
     */
    public function add()
    {
        $model = new LabelModel;
        // 新增记录
        if ($model->add($this->postData())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑
     */
    public function edit($product_label_id)
    {
        // 详情
        $model = LabelModel::detail($product_label_id);
        // 更新记录
        if ($model->edit($this->postData())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除
     */
    public function delete($product_label_id)
    {
        $model = LabelModel::detail($product_label_id);
        if (!$model->remove()) {
            return $this->renderError('该标签使用中，删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 审核商品标签
     * @return \think\response\Json
     * @Author joey
     * @date 2024/3/27 10:55
     */
    public function audit()
    {
        $param  = $this->postData();
        $model  = new LabelModel;

        if (!isset($param['product_label_id'])) {
            return $this->renderError('商品标签id不能为空');
        }
        $param['product_label_id'] = is_array($param['product_label_id']) ? $param['product_label_id'] : [$param['product_label_id']];
        $detail      = $model->whereIn('product_label_id', $param['product_label_id'])->select();
        if ($detail->isEmpty()) {
            return $this->renderError('商品标签不存在');
        }
        $data = ['audit_status' => $param['audit_status'], 'remark' => $param['remark'] ?? ''];
        $num = 0;
        foreach ($detail as $item) {
            $res = $item->allowField(['audit_status', 'remark'])->save($data);
            if ($res) {
                $num++;
            }
        }
        return $this->renderSuccess($num > 0 ? "审核成功{$num}条" : '审核失败');
    }
}